<%@page import="java.util.ArrayList"%>
<%@page import="edu.scause.gs.model.GoodsInfo"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>导购</title>
</head>
<body onload="controlsub()">
	
	<table cellspacing="0" >
		<thead>
			<tr>
				<th></th>
				<th>商品ID</th>
				<th>管理员ID</th>
				<th>商品名称</th>
				<th>尺寸</th>
				<th>种类</th>
				<th>简介</th>
				<th>库存</th>
				<th>状态</th>
				<th>已销售量</th>
				<th>购买数量</th>
			</tr>
		</thead>
		<tbody>
			<s:iterator value="goodsList">
				<tr>
				
					<td><input name="cb" type="checkbox" onchange="controlcb()"></td>
					<td align="right"><s:property value="goodsId" /></td>
					<td align="right"><s:property value="adminId" /></td>
					<td align="right"><s:property value="goodsName" /></td>
					<td align="right"><s:property value="size" /></td>
					<td align="right"><s:property value="kind" /></td>
					<td align="right"><s:property value="introduce" /></td>
					<td align="right"><div><s:property value="stock" /></div></td>
					<td align="right"><p><s:property value="state" /></p></td>
					<td align="right"><s:property value="totalSales" /></td>
					<td>
					<form action="Clientorders" method="post" id="clientsorder" onsubmit="return check4stock();">
						
						<input type="hidden" name="bought_clientId" value="<%=session.getAttribute("clientid") %>">
						<input type="hidden" name="bought_goodsId" value="<s:property value="goodsId" />">
						<input type="hidden" name="orders.finished" value="0">
						<input type="hidden" name="orders.shoppingDate" >
						<input type="text" name="orders.amount" >
						<input type="submit" name="submitbtn" value="下单" >
					</form>
					</td>
				</tr>
			</s:iterator>
		</tbody>
	</table>

</body>

<script type="text/javascript">
	function controlsub() {
		var textarray = document.getElementsByName("orders.amount");
		var btn = document.getElementsByName("submitbtn");
		for (var i = 0; i < btn.length; i++) {
			textarray[i].disabled = true;
			btn[i].disabled = true;
		}
		var state = document.getElementsByTagName("p");
		var temptext = document.getElementsByName("orders.amount");
		var tempbtn = document.getElementsByName("submitbtn");
		var cbarray = document.getElementsByName("cb");
		for (var i = 0 ; i < state.length; i++) {
			if (state[i].innerHTML == "false") {
				temptext[i].style.display = "none";
				tempbtn[i].style.display = "none";
				cbarray[i].style.display = "none";
			}
		}
	}

	var flag = true;
	var order = null;
	function controlcb() {
		var cbarray = document.getElementsByName("cb");
		var textarray = document.getElementsByName("orders.amount");
		var btn = document.getElementsByName("submitbtn");
		if (flag) {
			for (var i = 0; i < cbarray.length; i++) {
				cbarray[i].disabled = true;
				textarray[i].disabled = true;
				btn[i].disabled = true;
				if (cbarray[i].checked) {
					flag = false;
					cbarray[i].disabled = false;
					textarray[i].disabled = false;
					btn[i].disabled = false;
					order = i;
					getShoppingDate();
				}
			}
		} else {
			for (var i = 0; i < cbarray.length; i++) {
				cbarray[i].disabled = false;
				textarray[i].disabled = true;
				btn[i].disabled = true;
				flag = true;
			}
		}
//      测试代码		
//		if (cbarray[2].checked) {
			
//			document.getElementById("yeye").disabled = true;
//		} else {
//			document.getElementById("yeye").disabled = false;
//		}
	}
	
	function check4stock() {
		var goodstock = document.getElementsByTagName("div");
		var amount = document.getElementsByName("orders.amount");
		var tempstock = goodstock[order].innerHTML - 0;
		var tempamount = amount[order].value - 0;
		if (tempamount == "") {
			alert("请输入购买数量！");
			return false;
		} else {
		if (tempstock < tempamount) {
			alert("购买数量不能超于库存！");
			amount[order].value = "";
			return false;
		} else {
			//alert(amount[order].value);
			return true;
		}
		}
		return false;
	}
	
	function getShoppingDate() {
		var date = new Date();
		var day = date.getDate();
		var month = date.getMonth() + 1;
		var year = date.getFullYear();
		var shoppingdate = document.getElementsByName("orders.shoppingDate");
		shoppingdate[order].value = year + "-" + month + "-" + day;
	}
</script>

</html>